package com.nciae.chain.handler.security;

import com.nciae.po.*;
import com.nciae.service.IHoursemasterSecurityQuestionService;
import com.nciae.service.IHoursemasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Component
@Order(2)
public class HourseMasterSecurityQuestionHandler implements QHandler {

    @Autowired
    private IHoursemasterSecurityQuestionService hoursemasterSecurityQuestionService;

    @Autowired
    private IHoursemasterService hoursemasterService;

    @Override
    public void service(SecurityQuestion securityQuestion, int option) {

        // teacher,add
        if (!Objects.isNull(securityQuestion.getTeacher()) && !Objects.isNull(securityQuestion.getTeacher().getId()) && option == 1) {
            HoursemasterSecurityQuestion hoursemasterSecurityQuestion = new HoursemasterSecurityQuestion();
            hoursemasterSecurityQuestion.setHousemasterId(securityQuestion.getHoursemaster().getId());
            hoursemasterSecurityQuestion.setSecurityQuestionId(securityQuestion.getId());
            hoursemasterSecurityQuestionService.add(hoursemasterSecurityQuestion);
        }

        // hoursemaster, find
        if (option == 2) {
            List<HoursemasterSecurityQuestion> hoursemasterSecurityQuestions = hoursemasterSecurityQuestionService.findBySecurityQuestionId(securityQuestion.getId());
            for(HoursemasterSecurityQuestion hoursemasterSecurityQuestion : hoursemasterSecurityQuestions) {
                Long housemasterId = hoursemasterSecurityQuestion.getHousemasterId();
                Hoursemaster hoursemaster = hoursemasterService.findHoursemasterById(housemasterId);
                securityQuestion.setHoursemaster(hoursemaster);
            }
        }
    }
}
